import http from '@ohos.net.http';
import wifiManager from '@ohos.wifiManager';
import ConfigType from './ReactNativeBlobUtilConfig';

enum RequestType {
  Form,
  SingleFile,
  AsIs,
  WithoutBody,
  Others
}

enum ResponseType {
  KeepInMemory,
  FileStorage
}

enum ResponseFormat {
  Auto,
  UTF8,
  BASE64
}

export default class ReactNativeBlobUtilReq {
  // parameter
  options: ConfigType;
  taskId: string;
  method: string;
  url: string;
  headers: Object;
  form: Array<any>;
  callback: (value: Array<any>) => void;

  // variable
  destPath: string;
  httpRequest: http.HttpRequest;
  responseFormat: ResponseFormat = ResponseFormat.Auto;
  rawRequestBodyArray: Array<any>;

  // http parameter
  expectDataType: RequestType

  constructor(options: ConfigType, taskId: string, method: string, url: string, headers: Object, form: Array<any>, callback: (value: Array<any>) => void) {
    this.options = options;
    this.method = method.toLowerCase();
    this.headers = headers;
    this.rawRequestBodyArray = form;
    this.callback = callback;
  }

  run() {
    this.httpRequest = http.createHttp();
    let ext = (this.options.appendExt == null || !!this.options.appendExt) ? "" : "." + this.options.appendExt;

    // find cached result if `key` property exists

    // if (this.options.key != null) {
    //   cacheKey = ReactNativeBlobUtilUtils.getMD5(this.options.key);
    //   if (cacheKey == null) {
    //      cacheKey = this.taskId;
    //   }
    //
    //   File file = new File(ReactNativeBlobUtilFS.getTmpPath(cacheKey) + ext);
    //
    //   if (file.exists()) {
    //      invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, file.getAbsolutePath());
    //      return;
    //   }
    // }

    if (this.options.path) {
      this.destPath = this.options.path;
    } else if (this.options.fileCache) {
      // this.destPath = ReactNativeBlobUtilFS.getTmpPath(cacheKey) + ext;
    }

    try {
      // use trusty SSL socket
      if (this.options.trusty) {
        // clientBuilder = ReactNativeBlobUtilUtils.getUnsafeOkHttpClient(client);
      } else {
        // clientBuilder = client.newBuilder();
      }

      if (this.options.wifiOnly) {
        let isWifiConnect = false;
        if (wifiManager.isWifiActive() && wifiManager.isConnected()) {
          isWifiConnect = true;
        }

        if (!isWifiConnect) {
          // this.callback()
        }
      }

      if (this.headers) {
        for (let name in this.headers) {
          if (name.toLowerCase() === 'RNFB-Response') {
            let resValue = this.headers[name].toLowerCase();
            if (resValue === 'base64') {
              this.responseFormat = ResponseFormat.BASE64;
            } else if (resValue === 'utf8') {
              this.responseFormat = ResponseFormat.UTF8;
            }
          }
        }
      }

      if (this.method === 'put' || this.method === 'post' || this.method === 'patch') {
        if (this.rawRequestBodyArray) {

        }


      }

    } catch (err) {

    }
  }
}
